﻿using System;
using System.IO;
using System.Reflection;
using System.Windows;

/// <summary>
/// Class contains informations compiled into application(AssemblyInfo).
/// </summary>
public static class AppInfo {
	/// <summary>
	/// Field AssemblyTitle
	/// </summary>
	public static string Title {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			object[ ] attributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
			if (attributes.Length == 0) {
				return System.IO.Path.GetFileNameWithoutExtension(assembly.CodeBase);
			}

			return ((AssemblyTitleAttribute)attributes[0]).Title;
		}
	}

	/// <summary>
	/// Field AssemblyDescription
	/// </summary>
	public static string Description {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			object[ ] attributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
			if (attributes.Length == 0) {
				return "";
			}

			return ((AssemblyDescriptionAttribute)attributes[0]).Description;
		}
	}

	/// <summary>
	/// Field AssemblyCompany
	/// </summary>
	public static string Company {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			object[ ] attributes = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
			if (attributes.Length == 0) {
				return "";
			}

			return ((AssemblyCompanyAttribute)attributes[0]).Company;
		}
	}

	/// <summary>
	/// Field AssemblyProduct
	/// </summary>
	public static string Product {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			object[ ] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
			if (attributes.Length == 0) {
				return "";
			}

			return ((AssemblyProductAttribute)attributes[0]).Product;
		}
	}

	/// <summary>
	/// Field AssemblyCopyright
	/// </summary>
	public static string Copyright {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			object[ ] attributes = assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
			if (attributes.Length == 0) {
				return "";
			}

			return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
		}
	}

	/// <summary>
	/// Field AssemblyVersion
	/// </summary>
	public static Version Version {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			AssemblyName assemblyName = new AssemblyName(assembly.FullName);

			return assemblyName.Version;
		}
	}

	/// <summary>
	/// Contains date and time of release application, computed from automatic AssemblyVersion (x.x.*) eg. 1.0.*
	/// </summary>
	public static DateTime ReleasionDate {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			AssemblyName assemblyName = new AssemblyName(assembly.FullName);

			return new DateTime(2000, 1, 1).AddDays(assemblyName.Version.Build).AddSeconds(assemblyName.Version.Revision * 2).AddHours(-1);
		}
	}

	/// <summary>
	/// Contaions FileInfo about current exacutable file path
	/// </summary>
	public static FileInfo ExecutablePath {
		get {
			var assembly = Assembly.GetCallingAssembly( );
			if (Application.Current != null)
				assembly = Application.Current.GetType( ).Assembly;

			return new FileInfo(assembly.Location);
		}
	}
}
